package day12;

import day12.handler.AuthHandler;
import day12.handler.IMIdleStateHandler;
import day12.handler.PacketCodecHandler;
import day12.handler.simpleChannelHandler.IMHandler;
import day12.handler.simpleChannelHandler.request.*;
import day12.handler.spliter.Spliter;
import day12.util.coder.PacketDecoder;
import day12.util.coder.PacketEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.TimeUnit;

/**
 * @author chenshijie
 * @date 2023/12/23 16:24
 */

public class NettyServer {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        try {
            serverBootstrap
                    .group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {

                            ch.pipeline().addLast(new IMIdleStateHandler());
//                        ch.pipeline().addLast(new IdleStateHandler(3,4,5, TimeUnit.SECONDS));
                            ch.pipeline().addLast(new Spliter());
                            ch.pipeline().addLast(PacketCodecHandler.INSTANCE);
                            ch.pipeline().addLast(LoginRequestHandler.INSTANCE);
                            ch.pipeline().addLast(HeartBeatRequestHandler.INSTANCE);
                            ch.pipeline().addLast(AuthHandler.INSTANCE);
                            ch.pipeline().addLast(IMHandler.INSTANCE);
                        }
                    });

            ChannelFuture f = bind(serverBootstrap, 8001);
            f.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }

    private static ChannelFuture bind(final ServerBootstrap serverBootstrap, final int port) throws InterruptedException {

        ChannelFuture f = serverBootstrap
                .bind(port)
                .addListener(new GenericFutureListener<Future<? super Void>>() {
                    @Override
                    public void operationComplete(Future<? super Void> future) throws Exception {
                        if (future.isSuccess()) {
                            System.out.println("端口 [" + port + "] 绑定成功！");
                        } else {
                            System.out.println("端口 [" + port + "] 绑定失败！");
                            bind(serverBootstrap, port + 1);
                        }
                    }
                })
                .sync();

        return f;
    }
}
